package com.bistu.myplayer_zjr;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.List;

public class SongTools {

    //自动从媒体库中取得歌曲
    public List<Song> findSongs(Context context){

        //query：表、列名、查询条件、占位符、排序方式
        //MediaStore：Android系统提供的一个多媒体数据库，包括了多媒体数据库的所有信息，包括音频，视频和图像。
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        List<Song> songs = new ArrayList<Song>();

        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));    //歌名
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); //歌手
            String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));   //专辑名
            Long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); //时长
            int album_id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));   //专辑ID

            Bitmap front = getAlbumBitmap(context, album_id);   //封面，位图类型
            String file_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); //歌曲文件的名称
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//路径

            if (file_name.endsWith(".mp3")){
                Song song = new Song();
                song.setName(name);
                song.setAlbum(album);
                song.setDuration(duration);
                song.setArtist(artist);
                song.setFront(front);
                song.setPath(path);
                songs.add(song);
            }
        }
        cursor.close();
        return songs;
    }

    //获取歌曲封面信息
    public Bitmap getAlbumBitmap(Context context, int album_id){
        String uri = "content://media/external/audio/albums/" + Integer.toString(album_id);
        String album_art = null;
        Bitmap bm = null;
        Cursor album_cursor = context.getContentResolver().query(
                Uri.parse(uri), new String[]{"album_art"},
                null, null, null);
        if(album_cursor.getCount() > 0 && album_cursor.getColumnCount() > 0){
            album_cursor.moveToNext();
            album_art = album_cursor.getString(0);
        }
        album_cursor.close();
        if(album_art != null){
            bm = BitmapFactory.decodeFile(album_art);   //decodeFile(String pathName)，解析成位图对象
        }else {
            return null;
        }
        return bm;
    }

    //将毫秒转换为分：秒的形式
    public String getCurTime(int duration){
        String time = "";
        int min = duration/1000/60;
        int sec = duration/1000%60;

        time += min + ":";
        if(sec < 10){
            time += "0";
        }
        time += sec;
        return time;
    }
}
